package com.zhuifeng.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhuifeng.product.domain.Brand;
import com.zhuifeng.product.service.IBrandService;
import com.zhuifeng.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhuifeng.common.log.annotation.Log;
import com.zhuifeng.common.log.enums.BusinessType;
import com.zhuifeng.common.security.annotation.RequiresPermissions;

import com.zhuifeng.common.core.web.controller.BaseController;
import com.zhuifeng.common.core.web.domain.AjaxResult;
import com.zhuifeng.common.core.utils.poi.ExcelUtil;
import com.zhuifeng.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 品牌Controller
 *
 * @author zhuifeng
 * @date 2022-08-18
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController
{
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌列表
     */
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand)
    {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    @PostMapping("uploadLogo")
    public String uploadLogo(@RequestParam("file") MultipartFile multipartFile) {
        String s = OssUtil.uploadMultipartFile(multipartFile);
        return s;
    }

    /**
     * 导出品牌列表
     */
    @RequiresPermissions("product:brand:export")
    @Log(title = "品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brand brand)
    {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        util.exportExcel(response, list, "品牌数据");
    }

    /**
     * 获取品牌详细信息
     */
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable("brandId") String brandId)
    {
        return AjaxResult.success(brandService.selectBrandByBrandId(brandId));
    }

    /**
     * 获取品牌详细信息
     */
    @RequiresPermissions("product:brand:getBrandList")
    @GetMapping(value = "getBrandList/{typeId}")
    public AjaxResult getBrandList(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(brandService.selectBrandByTypeId(typeId));
    }

    /**
     * 新增品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand)
    {
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     * 修改品牌
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand)
    {
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
    public AjaxResult remove(@PathVariable String[] brandIds)
    {
        return toAjax(brandService.deleteBrandByBrandIds(brandIds));
    }

    @GetMapping("/findAllBrand")
    public List<Brand> findAllBrand(){
        List<Brand> list = brandService.findAllBrand();

        return list;
    }

    @PutMapping("elSwitch")
    public Boolean elSwitch(@RequestBody Brand brand) {
        boolean b = brandService.elSwitch(brand);
        return b;
    }
}
